<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="图片描述" prop="rentalDesc">
        <el-input
          v-model="queryParams.rentalDesc"
          placeholder="请输入图片描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片URL" prop="rentalUrl">
        <el-input
          v-model="queryParams.rentalUrl"
          placeholder="请输入图片URL"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"

        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['construction:pic:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"

        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="picList" @selection-change="handleSelectionChange" @row-dblclick="getRentalLabel">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="图片编号" align="center" prop="rentalPicId" />
      <el-table-column label="租赁服务编号" align="center" prop="rentalId" />
      <el-table-column label="图片描述" align="center" prop="rentalDesc" />
      <el-table-column label="图片URL" align="center" prop="rentalUrl" >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.rentalUrl"
            @click="getBigCertificateUrl(scope.row)"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="最后修改时间" align="center" prop="modifiedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifiedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租赁服务图片信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图片描述" prop="rentalDesc">
          <el-input v-model="form.rentalDesc" placeholder="请输入图片描述" />
        </el-form-item>

        <el-form-item label="图片URL" prop="rentalUrl">
          <el-upload
            class="upload-demo"
            :http-request="upload"
            action="aaa"
            accept=".jpg,.png"
            :limit="1"
            :file-list="InfoImgList"
            :on-remove="handleRemoveInfoImage"
            :before-remove="beforeRemoveInfo"
            :on-error="handleError"
            ref="upload">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">仅支持jpg,png格式,以最新上传为准</div>
          </el-upload>

          <!--<el-input v-model="formPic.picUrl" placeholder="请输入图片URL" />-->
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPic, getPic, delPic, addPic, updatePic, exportPic,uploadRental } from "@/api/construction/pic";
import {uploadImage} from "@/api/shopping/product";
export default {
  name: "Pic",
  data() {
    return {
      //添加修改图片
      InfoImgList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租赁服务图片信息表格数据
      picList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rentalId: null,
        rentalDesc: null,
        rentalUrl: null,
        modifiedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rentalId: [
          { required: true, message: "租赁服务ID不能为空", trigger: "blur" }
        ],
        rentalUrl: [
          { required: true, message: "图片URL不能为空", trigger: "blur" }
        ],
        modifiedTime: [
          { required: true, message: "最后修改时间不能为空", trigger: "blur" }
        ]
      },
      rentalId:null,
      fileUrl:null,
      dialogVisible:false,
      dialogImageUrl:"",
      dialogCertificateUrlList:[],
    };
  },
  created() {
    // 租赁服务ID
    let rentalId = parseInt(this.$route.query.rentalId);
    this.rentalId = rentalId;
    this.getList(rentalId);
  },
  methods: {


    upload(param){
      let file = param.file;
      let fileType = file.type;
      let isLt2M = file.size / 1024 / 1024 < 500;
      if (!isLt2M) {
        this.$message.error("上传大小大小必须小于500M");
        this.fileList=[];
        return;
      }
      let form = new FormData();
      form.append("file", file);
      uploadImage(form).then((res)=>{
        if(res.code==200){
          this.form.rentalUrl = "http://51jhf.com/profile/xiazai/"+res.filePath;
        }
        // console.log(res);
      })
    },

    //移除产品信息图片
    handleRemoveInfoImage(file, fileList){
      const picName = `${file.name}`;
      deleteDetailsImgByName(picName).then((res)=>{
        if(res.code == 200){
          this.form.rentalUrl = '';
          this.fileList=[];
        }

      });
    },
    beforeRemoveInfo(file, InfoImgList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },

    //上传异常
    handleError(err, file, fileList){
      this.$message.error("上传失败");
    },

    getBigCertificateUrl(row){
      this.dialogCertificateUrlList.push(row.rentalUrl);
    },
    /** 查询租赁服务图片信息列表 */
    getList(rentalId) {
      this.loading = true;
      this.queryParams.rentalId = rentalId;
      listPic(this.queryParams).then(response => {
        this.picList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rentalPicId: null,
        rentalId: null,
        rentalDesc: null,
        rentalUrl: null,
        modifiedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(this.rentalId);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rentalPicId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租赁服务图片信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rentalPicId = row.rentalPicId || this.ids
      getPic(rentalPicId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改租赁服务图片信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rentalPicId != null) {
            updatePic(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList(this.rentalId);
              }
            });
          } else {
            this.form.rentalId = this.rentalId;
            addPic(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList(this.rentalId);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rentalPicIds = row.rentalPicId || this.ids;
      this.$confirm('是否确认删除租赁服务图片信息编号为"' + rentalPicIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPic(rentalPicIds);
        }).then(() => {
          this.getList(this.rentalId);
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有租赁服务图片信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPic(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    // beforeUpload(file){
    //   this.fileUrl = file;
    //   if (file.type.indexOf("image/") == -1) {
    //     this.msgError("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。");
    //   } else {
    //     const reader = new FileReader();
    //     reader.readAsDataURL(file);
    //     reader.onload = () => {
    //       this.dialogVisible = true;
    //       this.dialogImageUrl = reader.result;
    //     };
    //   }
    // },
    // 上传图片
    // uploadImg() {
    //   let formData = new FormData();
    //   formData.append("rentalFile", this.fileUrl);
    //   uploadRental(formData).then((res) =>{
    //     if(res.code == 200){
    //       this.form.rentalUrl = "http://39.98.237.139"+res.data;
    //       this.msgSuccess("上传成功");
    //     }
    //   })
    // },

    getRentalLabel(row){
      this.$router.push({path:"/rentalLabel/label",query:{rentalId:this.rentalId,rentalPicId:row.rentalPicId}});
    },
  }
};
</script>
